package mark;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.jaaksi.commondemo.R;

/**
 * 创建时间：2018年02月11日14:33 <br>
 * 作者：fuchaoyang <br>
 */

public class MyAdapter extends BaseListAdapter<String> {
  StickyMark mStickyMark;
  private View vrView;
  private int vrPosition = -1;

  public MyAdapter(Context context) {
    super(context);
    mStickyMark = new StickyMark((Activity) context);
    mStickyMark.setMarkView(LayoutInflater.from(context).inflate(R.layout.popup_mark, null));
  }

  @Override public void notifyDataSetChanged() {
    vrView = null;
    vrPosition = -1;
    super.notifyDataSetChanged();
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    String item = getItem(position);
    if (vrPosition == -1 && "vr".equals(item)) {
      vrPosition = position;
    }
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.item_mark, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    if (vrView == convertView) { // vrview被复用干掉
      if (position != vrPosition) {
        vrView = null;
        mStickyMark.remove();
      }
    }

    if (position == vrPosition) {
      vrView = convertView;
      mStickyMark.attach(vrView);
    }

    holder.mTextView.setText(getItem(position));
    return convertView;
  }

  public void onScroll() {
    if (vrView != null) {
      mStickyMark.attach(vrView);
    }
  }

  public static class ViewHolder {
    public TextView mTextView;

    public ViewHolder(View view) {
      mTextView = (TextView) view.findViewById(R.id.textview);
    }
  }
}
